#include "hwctl.h"
#include "wiringPi.h"
#include "stddef.h"

hw_pt dev_add(hw_pt devHead, hw_pt target)
{
    if (NULL == devHead)
    {
        devHead = target;
    }
    else
    {   
        target->next = devHead;
        devHead = target;
    }
    return devHead;
}

hw_pt dev_find(hw_pt devHead, int key)
{
    if (NULL == devHead)
    {
        return NULL;
    }
    hw_pt target = devHead;
    while (NULL != target)
    {
        if (target->key == key)
        {
            return target; // 找到匹配项后立即返回
        }
        target = target->next;
    }
    return NULL; // 明确表示没有找到匹配项
}

int set_dev(hw_pt devHead)
{
    if (NULL == devHead)
    {
        return -1;
    }
    if (-1 != devHead->gpio_pin)
    {
        if (-1 != devHead->gpio_mode)
        {
            pinMode(devHead->gpio_pin, devHead->gpio_mode); // 设置GPIO模式
        }
        if (-1 != devHead->gpio_status)
        {
            digitalWrite(devHead->gpio_pin, devHead->gpio_status); // 设置GPIO状态,高低电
        }
    }
    return 0;
}
